Object-Oriented Programming<Home> |
|---|
Object-oriented programming is a method of programming based on a hierarchy of classes, and well-defined and cooperating objects.
A class is a structure that defines the data and the methods to work on that data. When you write programs in the Java language, all program data is wrapped in a class, whether it is a class you write or a class you use from the Java platform API libraries.
Classes in the Java platform API libraries define a set of objects that share a common structure and behavior. The java.lang.System class used in the example defines such things as standard input, output, and error streams, and access to system properties. In contrast, the java.lang.String class defines character strings.
In the example, you do not see an explicit use of the String class, but in the Java language, a character string can be used anywhere a method expects to receive a String object. During execution, the Java platform creates a String object from the character string passed to the System.out.println call, but your program cannot call any of the String class methods because it did not instantiate the String object.
If you want access to the String methods, you can rewrite the example program to create a String object as follows. This way, you can call a method such as the String.concat method that adds text to the original string.
An instance is an executable copy of a class. Another name for instance is object. There can be any number of objects of a given class in memory at any one time.
In the last example, four different String objects are created for the concatenation operation, text object, text2 object, and a String object created behind the scenes from the " that uses classes and objects" character string passed to the String.concat method.
One object-oriented concept that helps objects work together is inheritance. Inheritance defines relationships among classes in an object-oriented language. In the Java programming language, all classes descend from java.lang.Object and implement its methods.
The following diagram shows the class hierarchy as it descends from java.lang.Object for the classes in the user interface example above. The java.lang.Object methods are also shown because they are inherited and implemented by all of its subclasses, which is every class in the Java API libraries. java.lang.Object defines the core set of behaviors that all classes have in common.
As you move down the hierarchy, each class adds its own set of class-specific fields and methods to what it inherits from its superclass or superclasses. The java.awt.swing.JFrame class inherits fields and methods from java.awt.Frame, which inherits fields and methods from java.awt.Container, which inherits fields and methods from java.awt.Component, which finally inherits from java.lang.Object, and each subclass adds its own fields and methods as needed.
Another way objects work together is to define methods that take other objects as parameters. You get even more cooperation and efficiency when the objects are united by a common superclass. All classes in the Java programming language have an inheritance relationship.
For example, if you define a method that takes a java.lang.Object as a parameter, it can accept any object in the entire Java platform. If you define a method that takes ajava.awt.Component as a parameter, it can accept any component object. This form of cooperation is called polymorphism.